package com.shenmazong.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.user.pojo.TbSysUser;
import com.shenmazong.user.service.TbSysUserService;
import com.shenmazong.user.mapper.TbSysUserMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.utils.TokenUtils;
import com.shenmazong.vo.IdVo;
import com.shenmazong.vo.LoginUserInfoVo;
import com.shenmazong.vo.PageInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 *
 */
@Service
public class TbSysUserServiceImpl extends ServiceImpl<TbSysUserMapper, TbSysUser>
    implements TbSysUserService{

    @Autowired
    TbSysUserMapper tbSysUserMapper;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public ResultResponse add(TbSysUser user) {
        // 检查用户名
        TbSysUser one = tbSysUserMapper.selectOne(
                new QueryWrapper<TbSysUser>().lambda().eq(TbSysUser::getUserName, user.getUserName()).last("limit 1")
        );
        if(one != null) {
            return ResultResponse.FAILED(-1, "用户已经存在了");
        }

        // 添加用户
        String pass = user.getUserPass();
        String hashpw = BCrypt.hashpw(pass, BCrypt.gensalt());
        user.setUserPass(hashpw);

        tbSysUserMapper.insert(user);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse del(IdVo idVo) {
        // 判断用户存不存在
        TbSysUser tbSysUser = tbSysUserMapper.selectById(idVo.getId());
        if(tbSysUser == null) {
            return ResultResponse.FAILED(-1, "用户不存在");
        }

        // 删除用户
        tbSysUserMapper.deleteById(idVo.getId());
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse set(TbSysUser user) {
        // 检查用户名
        TbSysUser one = tbSysUserMapper.selectById(user.getUserId());
        if(one == null) {
            return ResultResponse.FAILED(-1, "用户不存在");
        }

        // 修改用户
        String pass = user.getUserPass();
        if(pass != null) {
            String hashpw = BCrypt.hashpw(pass, BCrypt.gensalt());
            user.setUserPass(hashpw);
        }

        tbSysUserMapper.updateById(user);

        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse getSysUserById(IdVo idVo) {
        // 判断用户存不存在
        TbSysUser tbSysUser = tbSysUserMapper.selectById(idVo.getId());
        if(tbSysUser == null) {
            return ResultResponse.FAILED(-1, "用户不存在");
        }

        // 脱密处理
        tbSysUser.setUserPass(null);

        return ResultResponse.SUCCESS(tbSysUser);
    }

    @Override
    public ResultResponse getSysUserByPage(PageInfoVo pageInfoVo) {

        Page<TbSysUser> page = new Page<>(pageInfoVo.getPageNo(), pageInfoVo.getPageSize());
        Page<TbSysUser> tbSysUserPage = tbSysUserMapper.selectPage(page, null);

        return ResultResponse.SUCCESS(tbSysUserPage);
    }

    @Override
    public ResultResponse loginByUserName(LoginUserInfoVo loginUserInfoVo) {
        // 判断用户是否存在
        QueryWrapper<TbSysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbSysUser::getUserName, loginUserInfoVo.getUsername());
        queryWrapper.lambda().last("limit 1");
        TbSysUser one = tbSysUserMapper.selectOne(queryWrapper);
        if(one == null) {
            return ResultResponse.FAILED(-1, "用户不存在");
        }

        // 判断用户密码
        String password = loginUserInfoVo.getPassword();
        boolean checkpw = BCrypt.checkpw(password, one.getUserPass());
        if(!checkpw) {
            return ResultResponse.FAILED(-1, "密码不正确");
        }

        // 生成token
        String token = TokenUtils.token()
                .setKey("1907a")
                .createToken("userId", ""+one.getUserId(), "userName", one.getUserName());
        one.setAccessToken(token);

        // 放到redis中
        redisTemplate.opsForValue().set("login_sysuser_"+one.getUserId(), token, 12, TimeUnit.HOURS);

        // 脱密
        one.setUserPass(null);

        return ResultResponse.SUCCESS(one);
    }
}




